package com.gitee.wsl.struct.command.parameters

import com.gitee.wsl.struct.command.model.command.context
import com.gitee.wsl.struct.command.model.parameters.options.delegate.option
import com.gitee.wsl.struct.command.testing.TestCommand
import com.gitee.wsl.struct.command.testing.parse
import com.gitee.wsl.test.forAll
import com.gitee.wsl.test.row
import com.gitee.wsl.test.shouldBe

import kotlin.test.Test

class EnvvarInferTest {
    @Test
    fun inferEnvvar() = forAll(
        row(arrayOf("--foo"), null, null),
        row(arrayOf("--bar"), "FOO", "FOO_BAR"),
        row(arrayOf("/bar"), "FOO", "FOO_BAR"),
        row(arrayOf("-b"), "FOO", "FOO_B"),
        row(arrayOf("-b", "--bar"), "FOO", "FOO_BAR")
    ) { names, prefix, expected ->
        class C : TestCommand(autoCompleteEnvvar = null) {
            val o by option(*names)

            init {
                context {
                    // Return the key as the value of the envvar
                    readEnvvar = { it }
                    autoEnvvarPrefix = prefix
                }
            }
        }
        C().parse("").o shouldBe expected
    }
}
